package com.cr.admin.business.news.controller;


import com.cr.admin.business.news.pojo.entity.News;
import com.cr.admin.business.news.pojo.param.NewsCreateParam;

import com.cr.admin.business.news.pojo.query.NewsQuery;
import com.cr.admin.business.news.pojo.vo.NewsCategoryVO;
import com.cr.admin.business.news.pojo.vo.NewsDetailsVO;
import com.cr.admin.business.news.pojo.vo.NewsStatusVO;
import com.cr.admin.business.news.service.NewsService;

import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "85.资讯管理")
@RestController
@Slf4j
@RequestMapping("/api/bus/news")
public class NewsController {

    @Autowired
    private NewsService newsService;


    @ApiOperation("获取资讯类别")
    @GetMapping("/category")
    public JsonResult getNewsCategory() {
        List<NewsCategoryVO> newsCategory = newsService.getNewsCategory();
        return JsonResult.success(newsCategory);
    }

    @ApiOperation("获取资讯状态")
    @GetMapping("/status")
    public JsonResult getNewsStatus() {
        List<NewsStatusVO> newsStatus = newsService.getNewsStatus();
        return JsonResult.success(newsStatus);
    }

    @ApiOperation("获取资讯详情")
    @GetMapping("/{id}")
    public JsonResult getNewsDetails( @PathVariable("id") Integer id) {
        NewsDetailsVO newsDetails = newsService.getNewsDetails(id);
        return JsonResult.success(newsDetails);
    }


    @ApiOperation("资讯创建")
    @PostMapping("/saveCreate")
    public JsonResult saveCreate(@RequestBody NewsCreateParam param) {
        newsService.saveCreate(param);
        return JsonResult.success();
    }

    @ApiOperation("发布资讯")
    @PostMapping("/send")
    public JsonResult sendNews(@RequestParam("id") int id) {
        newsService.sendNews(id);
        return JsonResult.success();
    }


    @ApiOperation("删除资讯")
    @DeleteMapping("/del")
    public JsonResult deleteNews(@RequestParam("ids") List<Long> ids) {
        newsService.deleteNews(ids);
        return JsonResult.success();
    }


    @ApiOperation(value = "查询文章列表")
    @GetMapping()
    public JsonResult list(NewsQuery newsQuery) {
        log.info("查询文章列表:{}", newsQuery);
        PageData<News> pageData = PageData.page(newsQuery.getPageNum(), newsQuery.getPageSize(), () -> newsService.list(newsQuery));
        return JsonResult.success(pageData);
    }

}
